package exfist;

/**
  * This code was written for a space invaders tutorial, the authors details are below 
  * 
  * Curso B?sico de desarrollo de Juegos en Java - Invaders
  * 
  * (c) 2004 Planetalia S.L. - Todos los derechos reservados. Prohibida su reproducci?n
  * 
  * http://www.planetalia.com
  * 
  */

import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.HashMap;

import javax.imageio.ImageIO;

public class SpriteCache 
{
	private HashMap sprites;
	
	protected SpriteCache() 
	{
		sprites = new HashMap();
	}
	
	private BufferedImage loadImage(String spriteName) 
	{
		URL url=null;
		try 
		{
			url = getClass().getClassLoader().getResource(spriteName);
			return ImageIO.read(url);
		} 
		catch (Exception e)
		{
			System.out.println("The image " +spriteName+" was not found at "+url);
			System.exit(0);
			return null;
		}
	}
	
	public BufferedImage getSprite(String spriteName) 
	{
		BufferedImage img = (BufferedImage)sprites.get(spriteName);
		if (img == null) 
		{
			img = loadImage("Imgs/"+spriteName);
			sprites.put(spriteName,img);
		}
		return img;
	}
	
}
